package org.little.jpa.service.impl;

import org.little.jpa.controller.UserController;
import org.little.jpa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from USER where NAME = ?", name);
    }

    @Override
    public Integer getAllUsers() {
        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from USER");
    }

    @Override
    @Transactional
    public void testInsertUser() {
        for(int i=0;i<10;i++){
            String name = "";
            int age = 0;
            if (i==5){
                name = "cesh111111111111111111i"+i;
//                age = Integer.parseInt(name);
                age = i;
            }else{
                name = "ceshi"+i;
                age = i;
            }

//            int age = i;
            LOG.info("insert into USER(NAME, AGE) values({}, {})",name,age);
            jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);

        }

    }
}
